package com.example.designmodule.observer;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: Dijah
 * @Date: 2022/2/8 14:42
 * @Desc:
 */
public class Subject {

    private List<Observer> observers = new ArrayList<Observer>();

    private int state;

    public int getState(){
        return state;
    }

    public void setState(int state){
        this.state = state;
        notifyAllObservers();
    }

    //注册观察者
    public void attach(Observer observer){
        observers.add(observer);
    }

    //通知观察者们
    private void notifyAllObservers() {
        for (Observer observer : observers) {
            observer.update();
        }
    }

}
